home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5713 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: news.cs.tut.fi!usenet
  2. From: p150650@korppi.cs.tut.fi (Tero Pulkkinen)
  3. Newsgroups: comp.lang.c,rec.games.programmer
  4. Subject: Re: Speed question here...
  5. Date: 20 Feb 1996 14:42:26 +0200
  6. Organization: Tampere University of Technology, Finland
  7. Sender: p150650@korppi.cs.tut.fi
  8. Distribution: inet
  9. Message-ID: <xduenrq418t.fsf@korppi.cs.tut.fi>
  10. References: <4ftluh$1gkv@hearst.cac.psu.edu> <4g9rbg$3cl@ooze.val.net>
  11. NNTP-Posting-Host: korppi.cs.tut.fi
  12. NNTP-Posting-User: p150650
  13. In-reply-to: agriffini@vv.val.net's message of Mon, 19 Feb 1996 20:10:06 GMT
  14. X-Newsreader: Gnus v5.0.15
  15.  
  16.    >I was curious as to how fast something like the 
  17.    >following would execute:
  18.  
  19.    >    int x;
  20.    >    node *ptr;   ptr in linked list
  21.  
  22.    >       for ( ptr = first_node; ptr != NULL; ptr = ptr.next ) {
  23.    >       for ( x = 0; x < max; x++ ) {
  24.    >        array[x] = array_other[x];
  25.    >           }
  26.    >      } 
  27.  
  28.    >I really am not interested in the assignment statement, that's easy.
  29.    >but the traversal through the linked list, and inner integer incremental
  30.    >for loop for each node visited.  How fast is a traversal through
  31.    >a linked list when you do a for loop at each node?
  32.  
  33. You should be interested in the assignment, coz its the slowest part of that
  34. code. :) If the array item happens to be something else than sizeof 2^n then
  35. that assignment requires at least one multiply. :) Slow as hell. better
  36. do it this way:
  37.  
  38. for(arrayptr=array, array_otherptr=array_other, ptrend=arrayptr+max; 
  39.     arrayptr<ptrend;) {
  40.     *arrayptr++=*array_otherptr++; 
  41. }
  42.  
  43. That way it only does the multiplication (if it requires that) outside
  44. the loop instead of doing it every iteration. Usually that index-presentation
  45. is not too good, and the only posible place for multiplication in that one
  46. can be reduced out to use pointers at the first place instead of arbitrary
  47. index that has nothing to do with the data we really want to represent.
  48. (and here we really want to represent positions in an array, not some
  49. dummy integer values... :)
  50.  
  51. Indexing with constant value is ok with efficency, but if you need to put
  52. a variable to it, then you'd really consider if it can be done any other
  53. way.
  54.  
  55. And btw, if you want fast code, you should use do { } while() -construct
  56. instead of for(;;) -loop. That way you avoid having two jumps inside the
  57. loop. And if you have situation where you always dont have to execute the
  58. insides of the loop, then can do it with an if... Most compilers put the
  59. loop test to the start of the loop block, and then do unconditional jump
  60. to the start of it. That's one too much. :)
  61.  
  62. Propably the best way of doing the situation you had is like this:
  63.  
  64. array_type *p1=array,*p2=array_other,*pend=array_end;
  65. node *ptr;   //ptr in linked list
  66.  
  67. if (ptr)
  68. do {
  69.   do {
  70.     *p1++=*p2++;
  71.   } while (p1<pend);
  72.   ptr=ptr->next; // note your code had a bug here, using pointer it must be ->
  73. } while (ptr);
  74.  
  75. This code doesnt look nice, and these things should be only done at the last
  76. possible optimization you'll make, but in some cases the result might be
  77. like twice faster or something.
  78.  
  79. >  BTW: Both if this question has any sense or not (my opinion is that
  80. >  hasn't)... is anyway graphic related ?
  81.  
  82. ok, removed this from comp.graphics.algorithms, its now only at somehow
  83. proper groups...
  84.  
  85. -- Codex / Paranoids
  86. -- Tero Pulkkinen -- terop@kotka.cs.tut.fi --
  87.